﻿// <copyright file="BaseProvincesGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using LightSwitchApplication.Export;
    using LightSwitchApplication.Import;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class BaseProvincesGM
    {
        #region Handling BaseProvinces and DomainProvinces

        partial void BaseProvincesGM_Saved()
        {
            foreach (var baseProvince in this.Collection)
            {
                this.GenerateDomainProvince(baseProvince);
            }

            this.Neighbours.Refresh();
            this.InvertedNeighbours.Refresh();
            this.BaseProvinceNeighboursQuery.Refresh();
        }

        /// <summary>
        /// When a new BaseProvince is created, a DomainProvince related to it is generated automatically.
        /// </summary>
        /// <param name="baseProvince">The base province.</param>
        private void GenerateDomainProvince(BaseProvince baseProvince)
        {
            if (baseProvince.DomainProvince == null)
            {
                var domainProvince = this.DataWorkspace.ApplicationData.DomainProvinces.AddNew();

                domainProvince.Visibility = baseProvince.Visibility;
                domainProvince.BaseProvince = baseProvince;
                domainProvince.PopulationLevel = 0;
                domainProvince.FortificationLevel = 0;
                domainProvince.Loyalty = 0;

                this.DataWorkspace.ApplicationData.SaveChanges();
            }
        }

        #endregion

        #region Add/Edit/Delete BaseProvince

        partial void gridAddAndEditNew_Execute()
        {
            var province = this.Collection.AddNew();
            province.Visibility = (int)VisibilityEnum.Public;

            this.FindControl("ModalWindow").DisplayName = "Add new Province";
            this.OpenModalWindow("ModalWindow");
        }

        partial void gridEditSelected_CanExecute(ref bool result)
        {
            result = this.Collection.SelectedItem != null;
        }

        partial void gridEditSelected_Execute()
        {
            this.FindControl("ModalWindow").DisplayName = string.Format("Edit {0}", this.Collection.SelectedItem.Name);
            this.OpenModalWindow("ModalWindow");
        }

        partial void SubmitModalWindow_Execute()
        {
            this.CloseModalWindow("ModalWindow");
        }

        partial void CancelModalWindow_Execute()
        {
            this.CloseModalWindow("ModalWindow");
            this.Collection.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit/Delete Neighbour

        partial void BaseProvinceNeighboursAddAndEditNew_Execute()
        {
            var relation = this.Neighbours.AddNew();
            relation.StartProvince = this.Collection.SelectedItem;

            this.FindControl("ModalNeighbour").DisplayName = "Add new Neighbour";
            this.OpenModalWindow("ModalNeighbour");
        }

        partial void BaseProvinceNeighboursDeleteSelected_CanExecute(ref bool result)
        {
            result = this.BaseProvinceNeighboursQuery.SelectedItem != null;
        }

        partial void BaseProvinceNeighboursDeleteSelected_Execute()
        {
            bool found = false;
            foreach (var relation in this.Neighbours)
            {
                if (relation.StartProvince.Id == this.BaseProvinceNeighboursQuery.SelectedItem.Id)
                {
                    this.Neighbours.DeleteSelected();
                    found = true;
                    break;
                }
                else if (relation.EndProvince.Id == this.BaseProvinceNeighboursQuery.SelectedItem.Id)
                {
                    this.Neighbours.DeleteSelected();
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                foreach (var relation in this.InvertedNeighbours)
                {
                    if (relation.StartProvince.Id == this.BaseProvinceNeighboursQuery.SelectedItem.Id)
                    {
                        this.InvertedNeighbours.DeleteSelected();
                        break;
                    }
                    else if (relation.EndProvince.Id == this.BaseProvinceNeighboursQuery.SelectedItem.Id)
                    {
                        this.InvertedNeighbours.DeleteSelected();
                        break;
                    }
                }
            }

            this.DataWorkspace.ApplicationData.SaveChanges();
            this.BaseProvinceNeighboursQuery.Refresh();
        }

        partial void SubmitModalNeighbour_Execute()
        {
            this.CloseModalWindow("ModalNeighbour");
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Refresh the queries to take into account the new neighbour
            this.BaseProvinceNeighboursQuery.Refresh();

            // Try to generate a Domain Province associated with this base province
            this.GenerateDomainProvince(this.Collection.SelectedItem);
        }

        partial void CancelModalNeighbour_Execute()
        {
            this.CloseModalWindow("ModalNeighbour");
            this.Neighbours.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add Trait

        partial void RelationBaseProvinceTraitsAddAndEditNew_Execute()
        {
            this.RelationBaseProvinceTraits.AddNew();

            this.FindControl("ModalTrait").DisplayName = "Add existing Trait";
            this.OpenModalWindow("ModalTrait");
        }

        partial void SubmitModalTrait_Execute()
        {
            this.CloseModalWindow("ModalTrait");
        }

        partial void CancelModalTrait_Execute()
        {
            this.CloseModalWindow("ModalTrait");
            this.Neighbours.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        partial void ImportProvinces_Execute()
        {
            ImporterHelper.ImportBaseProvinces(this.DataWorkspace, this.Collection);
        }

        partial void ImportNeighbours_Execute()
        {
            ImporterHelper.ImportNeighbours(this.DataWorkspace, this.Collection);
        }

        partial void Export_Execute()
        {
            Exporter.ExportBaseProvincesAndNeighbours(this.Collection);
        }
    }
}
